﻿(function ($) {

    var methods = {
        init: function (options) {
            return this.each(function () {
                var settings = {
            };
            var $this = $(this),
                    data = $this.data('jCollapsiblePanel'),
                    jCollapsiblePanel = $('<div />', {
                });

            // If the plugin hasn't been initialized yet
            if (!data) {
                if (options) {
                    $.extend(settings, options);
                }
                $(this).data('jCollapsePanel', {
                    target: $this,
                    jCollapsiblePanel: jCollapsiblePanel,
                    settings: settings
                });
            };

            var Header = $(this).find(".collapsePanelHeader");
            var Content = $(this).find(".content");
            var btnArrow = $(this).find(".arrow");

            Header.bind('click.jCollapsiblePanel', { context: $(this) }, function (event) {
                Content.toggle('fast');
                btnArrow.toggleClass('arrowExpand');
                btnArrow.toggleClass('arrowClose');
                return false;
            });
        });
    },

    destroy: function () {
        return this.each(function () {
            var $this = $(this), data = $this.data('jCollapsiblePanel');

            $(window).unbind('.jCollapsiblePanel');
            data.jCollapsePanel.remove();
            $this.removeData('jCollapsiblePanel');
        })
    }
};


$.fn.jCollapsiblePanel = function (method) {
    if (methods[method]) {
        return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
    } else if (typeof method === 'object' || !method) {
        return methods.init.apply(this, arguments);
    } else {
        $.error('Method ' + method + ' does not exist on jQuery.jCollapsiblePanel');
    }
};

})(jQuery);
